// 控制页面登录权限(路由访问)的文件，做登录拦截
import router from './router'
import store from './store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

const whiteList = ['/login', '/404']
router.beforeEach(async(to, from, next) => {
  NProgress.start()
  if (store.getters.token) {
    // 登陆过
    if (to.path === '/login') {
      next('/')
      NProgress.done()
    } else {
      // 防止重复获取资料
      if (!store.state.user.userInfo.userId) {
        await store.dispatch('user/getUserInfo')
        // console.log('111', res)
      }
      next()
    }
  } else {
    // 未登录
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})
